/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myetl;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author apuangr1
 */
public class SourceData {

    String table;
    String[] cols;
    int columnCount;
    String[] colTypes;
    String criteria;
    String sql;
    String columns;
    AppManager apm;
    ResultSet result;
    Map<Integer, Column> colsInfo;
    String[] Values;

    SourceData(AppManager apm) {
        colsInfo = new HashMap<Integer, Column>();
        this.apm = apm;
        this.apm.setSourceData(this);
        String[] tempCols = this.apm.getSourceColumns().split(",");
        cols = new String[tempCols.length + 1];
        columnCount = tempCols.length;
        for (int i = 0; i < tempCols.length; i++) {
            cols[i + 1] = tempCols[i].trim();
            System.out.println(tempCols[i]);
        }
        this.table = apm.getSourceTable();
        this.criteria = apm.getSourceCriteria();
        this.columns = apm.getSourceColumns();
        buildStatement();
        retrieve();
    }

    private void buildStatement() {
        sql = "select " + columns + " from " + table + " where " + criteria;
        System.out.println(sql);
    }

    public void retrieve() {
        try {
            Statement stmt = this.apm.getSourceConn().createStatement();
            result = stmt.executeQuery(sql);

            ResultSetMetaData md = result.getMetaData();
            colTypes = new String[md.getColumnCount() + 1];
            int numberOfColumn = md.getColumnCount();
            for (int i = 1; i <= numberOfColumn; i++) {
                colTypes[i] = md.getColumnTypeName(i).toUpperCase();
                System.out.println(colTypes[i]);
            }


        } catch (SQLException ex) {
            ex.printStackTrace();
        }

    }

    public boolean moveNext() {
        try {
            return result.next();
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
            
        }
    }

    public String getString(int column) {
        try {
            if (!colTypes[column].contains("CHAR")) {
                //System.out.println("error: getstring source data");
                return "Error";
            }
            return result.getString(column);
        } catch (SQLException ex) {
            ex.printStackTrace();
            return "Error getString";
        }
    }

    public double getDecimal(int column) {
        try {
            if (!colTypes[column].contains("DEC")) {
                System.out.println("error: getDecimal");
                return -9999.9999;
            }
            return result.getDouble(column);
        } catch (SQLException ex) {
            return -9999.9999;
        }
    }
    public Date getDate(int column) {
        try {
            if (!colTypes[column].contains("DATE")) {
                System.out.println("error: date");
                return null;
            }
            
            return Command.getSqlDate(result.getDate(column));
        } catch (SQLException ex) {
            
            return null;
        }
    }

    public int getInteger(int column) {
        try {
            if (!colTypes[column].contains("INT")) {
                System.out.println("error: Integer");
                return -9999;
            }
            return result.getInt(column);
        } catch (SQLException ex) {
            return -9999;
        }
    }

    
    public String getColumnTypeNmae(int i){
        return this.colTypes[i];

    }
}
